{% import "utils.html" as utils %}
<form method="POST" action="/user/{{ userid }}/setnewpwd">
  <h2 class="text-center">用户列表</h2>
  <script src="{{ static_url('components/blueimp-md5/js/md5.js') }}"></script>
  <hr>
  <div id="run-result"></div>

  <div class="form-group">
    <label class="control-label" for="note" ><font color='red'>用户名：{{usermail}}</font></label><br>
    <label class="control-label" for="note">新密码：</label>
    <input type="text" class="form-control" name="newpwd" value="" id="newpwd" >
    <label class="control-label" for="note">重复新密码：</label>
    <input type="text" class="form-control" name="renewpwd" value="" id="renewpwd">
    <label class="control-label" for="note">管理员邮箱</label>
    <input type="text" class="form-control" name="adminmail" value="" id="adminmail" placeholder="必须输入账号密码验证">
    <label class="control-label" for="note">管理员密码</label>
    <input type='password' class="form-control" name="adminpwd" value="" id="adminpwd" placeholder="必须输入账号密码验证">
  </div>

  <div class="text-right">
    <button id="setnewpwdbtn" name="setnewpwdbtn" type="submit" data-loading-text="loading..." class="btn btn-primary">修改</button>
  </div>

  <script>
    $('#setnewpwdbtn').on('click', function() {
      data = {}
      NewPWD = $('#newpwd')[0].value;
      reNewPWD = $('#renewpwd')[0].value;
      if (NewPWD == reNewPWD){
        if (NewPWD.length >= 6){
          data['adminmail'] = $('#adminmail')[0].value;
          data['adminpwd'] = md5($('#adminpwd')[0].value);
          data['newpwd'] = NewPWD;
          data['usermail'] = "{{usermail}}";
        } else {
          $('#run-result').html("<h1 class=\"alert alert-danger text-center\">设置失败</h1><div><pre>密码少于6位</pre></div>").show();
          return
        }
      } else {
        $('#run-result').html("<h1 class=\"alert alert-danger text-center\">设置失败</h1><div><pre>密码不一致</pre></div>").show();
        return
      } 

      $.ajax("/user/{{ userid }}/setnewpwd", 
             {type: 'POST',
              data: data,})
        .done(function(data) {
          $('#run-result').html(data).show();})
      return false;
    });
  </script>

  {{ utils.submit_loading() }}
</form>

